package free.grnadav.jquerymobilegwt.client.button;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Widget;

import free.grnadav.jquerymobilegwt.client.hasinterface.HasIcon;

public class Button extends Composite implements HasText, HasIcon, HasClickHandlers {
	private static ButtonUiBinder uiBinder = GWT.create(ButtonUiBinder.class);

	interface ButtonUiBinder extends UiBinder<Widget, Button> {
	}

	
	public enum IconPosition  {
		RIGHT ("right"),
		LEFT ("left"),
		TOP ("top"),
		BOTTOM ("bottom");
		
		public final String jqmDataString;
		IconPosition(String jqmDataString) {
			this.jqmDataString = jqmDataString;
		}
	}
	
	@UiField
	Anchor button;
	
	private Icon icon = null;

	private IconPosition iconPosition;
	
	public Button() {
		initWidget(uiBinder.createAndBindUi(this));
		button.getElement().setAttribute("data-role", "button");
		button.setHref("#");
		setHasText(false);
	}

	public void setText(String text) {
		setHasText(true);
		button.setText(text);
	}

	public String getText() {
		return button.getText();
	}

	@Override
	public Icon getIcon() {
		return icon;
	}

	@Override
	public void setIcon(Icon icon) {
		this.icon = icon;
		button.getElement().setAttribute("data-icon", icon.jqmDataType);
	}
	
	public void setIconPosition(IconPosition iconPosition) {
		this.iconPosition = iconPosition;
		button.getElement().setAttribute("data-iconpos", iconPosition.jqmDataString);
	}
	
	public IconPosition getIconPosition() {
		return iconPosition;
	}
	
	private void setHasText(boolean hasText) {
		if (hasText) {
			button.getElement().removeAttribute("data-iconpos");
		} else {
			button.getElement().setAttribute("data-iconpos", "notext");
		}
	}

	@Override
	public HandlerRegistration addClickHandler(ClickHandler handler) {
		return button.addClickHandler(handler);
	}

}
